package com.itboy.mall.pms.categorybrand.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.exception.CustomException;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.mall.pms.brand.service.IMallBrandService;
import com.itboy.mall.pms.brand.vo.MallBrandVo;
import com.itboy.mall.pms.category.service.IMallCategoryService;
import com.itboy.mall.pms.category.vo.MallCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.categorybrand.bo.MallCategoryBrandAddBo;
import com.itboy.mall.pms.categorybrand.bo.MallCategoryBrandQueryBo;
import com.itboy.mall.pms.categorybrand.bo.MallCategoryBrandEditBo;
import com.itboy.mall.pms.categorybrand.domain.MallCategoryBrand;
import com.itboy.mall.pms.categorybrand.mapper.MallCategoryBrandMapper;
import com.itboy.mall.pms.categorybrand.vo.MallCategoryBrandVo;
import com.itboy.mall.pms.categorybrand.service.IMallCategoryBrandService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 品牌分类关联Service业务层处理
 *
 * @author itboy
 * @date 2021-07-14
 */
@Service
public class MallCategoryBrandServiceImpl extends ServicePlusImpl<MallCategoryBrandMapper, MallCategoryBrand> implements IMallCategoryBrandService {

	@Autowired
	private IMallCategoryService iMallCategoryService;

	@Autowired
	private IMallBrandService iMallBrandService;

	@Autowired
	private IMallCategoryBrandService iMallCategoryBrandService;

    @Override
    public MallCategoryBrandVo queryById(String id){
        return getVoById(id, MallCategoryBrandVo.class);
    }

    @Override
    public TableDataInfo<MallCategoryBrandVo> queryPageList(MallCategoryBrandQueryBo bo) {
        PagePlus<MallCategoryBrand, MallCategoryBrandVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallCategoryBrandVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallCategoryBrandVo> queryList(MallCategoryBrandQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallCategoryBrandVo.class);
    }

    private LambdaQueryWrapper<MallCategoryBrand> buildQueryWrapper(MallCategoryBrandQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallCategoryBrand> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getBrandId()), MallCategoryBrand::getBrandId, bo.getBrandId());
        lqw.like(StrUtil.isNotBlank(bo.getCatalogName()), MallCategoryBrand::getCatalogName, bo.getCatalogName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallCategoryBrandAddBo bo) {
        MallCategoryBrand add = BeanUtil.toBean(bo, MallCategoryBrand.class);
		MallBrandVo mallBrandVo = iMallBrandService.queryById(bo.getBrandId());
		MallCategoryVo mallCategoryVo = iMallCategoryService.queryById(bo.getCatalogId());
		if(ObjectUtil.isNull(mallBrandVo)&&ObjectUtil.isNull(mallCategoryVo)){
			throw  new CustomException("牌品或分类不存在!");
		}
		add.setBrandName(mallBrandVo.getName());
		add.setCatalogName(mallCategoryVo.getName());
		validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallCategoryBrandEditBo bo) {
        MallCategoryBrand update = BeanUtil.toBean(bo, MallCategoryBrand.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallCategoryBrand entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public List<MallCategoryBrandVo> queryListByCatelogId(String catalogId) {
		LambdaQueryWrapper<MallCategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallCategoryBrand::getCatalogId,catalogId);
		List<MallCategoryBrandVo> list = iMallCategoryBrandService.listVo(queryWrapper, MallCategoryBrandVo.class);
		return list;
	}
}
